﻿using System;
using Mbs.Reflection;

namespace Mbs.ComponentModel
{
    [Serializable]
    public class UndoableObject:ComponentObject
    {
        private readonly string ClassName;
        protected UndoableObject()
        {
            ClassName = GetType().Name;
        }

        protected override void OnPropertyChanged(string propertyName, object newValue, object oldValue)
        {

            if (IsEdit)
                return;

            base.OnPropertyChanged(propertyName, newValue, oldValue);

            UndoManager undoMgr = UndoManager.Current;
            if (undoMgr != null)
            {
                undoMgr.AddAction(new SimpleUndoAction
                {
                    Name = ClassName + "." + propertyName,
                    UndoHandler = () => this.SetProperty(propertyName, oldValue),
                    RedoHandler = () => this.SetProperty(propertyName, newValue),
                });
            }

        }   
    }
}
